﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ApplicationGenerator.Versions.BusinessLogic;
using System.Workflow.ComponentModel.Serialization;
using System.IO;
using ApplicationGenerator.DataAccess;
using System.Xml;

namespace WorkflowLibrary.DataAccess
{
	public class WLDataAccessProxy
	{
    
        #region Variables

        private static volatile WLDataAccessProxy instance;
        private string _ConnectionString = null;
        private static object syncObj = new Object();

        #endregion
   

        #region Construction

        /// <summary>
        /// Default private constructor
        /// </summary>
        private WLDataAccessProxy()
        {
        }

        #endregion

        #region Properties

        public static WLDataAccessProxy GetInstance(string _ConnectionString)
        {
            if (null == instance)
            {
                lock (syncObj)
                {
                    if (null == instance)
                    {
                        instance = new WLDataAccessProxy();            
                    }
                }
            }
            

            instance.ConnectionString = _ConnectionString;

            return instance;
        }

        public string ConnectionString
        {
            get { return _ConnectionString; }
            set { _ConnectionString = value; }
        }

        #endregion

        #region Methods

        public VersionInfo GetModelInfo(Guid workflowId)
        {

            byte[] xoml = (byte[])Serializer.Deserialize(Policy.Workflow, workflowId);
            System.Text.Encoding encoding = System.Text.Encoding.ASCII;
            string xomlString = encoding.GetString(xoml);

            XmlReader reader = XmlReader.Create(new StringReader(xomlString));

            WorkflowMarkupSerializer ser = new WorkflowMarkupSerializer();
            
            GeneratorSequentialWorkflow wf = (GeneratorSequentialWorkflow) ser.Deserialize(reader);

            return wf.MetaModel;
        }

        #endregion

    }
}
